# coding=utf-8

# 输入生成文件的要求

# 生成一笔资产，分期2期
# 桌面创建文件夹
import os
import time
import csv
import index
import getpass

userName = getpass.getuser()

basePath = '/Users/' + userName + '/Desktop/'
fileDirPath = basePath + 'testfile'
desktopPath = basePath

if os.path.exists(fileDirPath):
	os.chdir(fileDirPath)
else:
	path = desktopPath
	title = "testfile"
	new_path = os.path.join(path, title)
	if not os.path.isdir(fileDirPath):
		os.mkdir(fileDirPath)
		os.chdir(fileDirPath)

# 参数输入
# 输入生成文件的要求
testPurpose = str(input('请输入生成文件类型（1=资产生成，2=资产生成+结息，3=资产生成+转逾期，4=资产生成+还款，5=资产生成+结清）：'))
# 资产数据参数
totalAmt, disbDate, repayPeriod, termCnt, interRate = input('请输入合同金额/合同日期yyyymmdd/分期频率(天)/分期期数/利率(<1):').split('/')

# 固定参数维护
lenderOrgCode = 'ORG2018041500000000'
managerOrgCode = 'ORG2018041500000000'
prodCode = 'Yprod2018'

userId = 'User001'
repayPeriodType = 'D'
repayMethod = 'RM01'
yearDays = 360

# billID按时间生成，不得重复
batchNo = str(time.strftime("%m%d%I%M%S"))
billID = 'TestBill' + batchNo
print(billID)

# 计算合同截止日
timeTuple = time.strptime(disbDate, "%Y%m%d")
date_time = int(time.mktime(timeTuple))
endDate_Time = date_time + 86400 * int(termCnt) * int(repayPeriod)  # 一天86400秒
endDate_local = time.localtime(endDate_Time)
endDate = time.strftime("%Y%m%d", endDate_local)

today = int(time.strftime("%Y%m%d"))

# 计算分期本金和利息金额
prinAmt = round(float(totalAmt) / int(termCnt), 0)
intrAmt = round((float(totalAmt) * float(interRate)) / yearDays * int(repayPeriod), 0)

# 计算当前期次
todaySecs = int(time.mktime(time.strptime(str(today), "%Y%m%d")))
dayDiff = (todaySecs - date_time) / 86400
curTerm = int(dayDiff // int(repayPeriod)) + 1
print(curTerm)

# 计算截止至目前总利息
totalIntrBal = round(float(totalAmt) * float(interRate) / yearDays * dayDiff, 0)
#计算当期利息
curIntrBal= totalIntrBal-(curTerm-1)*intrAmt

def astCreat():
	indexNum = str(index.getIndex())
	cratePath = basePath + 'testfile/create'

	os.path.join(basePath, '/testfile/', 'create', indexNum)
	os.mkdir(cratePath + indexNum)
	os.chdir(cratePath + indexNum)
	print('creat' + indexNum)

	with open("文件总控_" + str(today) + ".csv", "w") as csvCreatList:
		writer = csv.writer(csvCreatList)
		writer.writerows([
			["文件名", "md5", "总数", "发生日期"],
			["合同_" + indexNum + ".csv", "md1", 10, today],
			["账单_" + indexNum + ".csv", "md1", 10, today],
			["分期_" + indexNum + ".csv", "md1", 10, today],
		])

	with open("合同_" + indexNum + ".csv", "w") as csvAstCreat001:
		writer = csv.writer(csvAstCreat001)
		writer.writerows([
			["用户id", "合同id", "合同类型", "手机号", "用户名", "证件号", "证件类型", "省份",
			 "城市", "贷款类型", "贷款用途", "账单id", "出资比例", "出资类型",
			 "原始权益机构", "代理机构", "产品码",
			 "放款金额", "发放日期", "还款方式", "还款周期", "还款周期类型", "年化天数", "生效日期",
			 "结束日期",
			 "利率", "总期次"],
			[userId, "Contract001", 'CT1', 1111, "Test1", 2222, "IDENTIFICATION", "Zhejiang", "Hangzhou",
			 "MORTGAGELOAN", "HOUSEPURCHASE", billID, 10, 'individual', lenderOrgCode, managerOrgCode, prodCode,
			 totalAmt, disbDate,
			 repayMethod, repayPeriod, repayPeriodType, yearDays, disbDate, endDate, interRate, termCnt], ])

	counterNum = int(1)
	install_startDate = disbDate
	termList = []
	intrCal = totalIntrBal
	lastTerm = float(totalAmt)
	while counterNum <= int(termCnt):
		periodTime = date_time + 86400 * counterNum * int(repayPeriod)  # 一天86400秒
		period_timeDate = time.localtime(periodTime)
		install_endDate = time.strftime("%Y%m%d", period_timeDate)
		if intrCal >= intrAmt:
			intrBal = 0
		else:
			intrBal = intrCal
		intrCal = max(intrCal - intrAmt, 0)

		if counterNum < curTerm:
			paidTerm = 0
			intrBal = 0
		elif counterNum == curTerm:
			paidTerm = prinAmt
			intrBal = totalIntrBal - intrAmt * (curTerm - 1)
		elif counterNum > curTerm:
			paidTerm = prinAmt
			intrBal = 0

		if counterNum == int(termCnt):
			termAst = [billID, counterNum, install_startDate, install_endDate, intrAmt, 0, lastTerm, intrBal, 0, 0, 0,
			           0,
			           "nor", 0,
			           lenderOrgCode]
		else:
			termAst = [billID, counterNum, install_startDate, install_endDate, intrAmt, 0, paidTerm, intrBal, 0, 0, 0,
			           0,
			           "nor", 0,
			           lenderOrgCode]
		lastTerm = lastTerm - prinAmt
		termList.append(termAst)
		install_startDate = install_endDate
		counterNum = counterNum + 1

	with open("分期_" + indexNum + ".csv", "w") as csvAstCreat003:
		writer = csv.writer(csvAstCreat003)
		writer.writerows([
			                 ["账单id", "期次号", "开始日期", "结束日期", "到期利息", "到期费用",
			                  "本金余额", "利息余额", "逾期本金余额", "逾期利息余额",
			                  "分期手续费余额", "分期滞纳金余额", "资产状态", "逾期天数", "原始权益机构"]
		                 ] + termList)

	unpaidIntr = totalIntrBal - ((curTerm - 1) * intrAmt)
	unpaidPrin = int(totalAmt) - ((curTerm - 1) * prinAmt)

	with open("账单_" + indexNum + ".csv", "w") as csvAstCreat002:
		writer = csv.writer(csvAstCreat002)
		writer.writerows([
			["用户id", "账单id", "资产类型", "原始权益机构", "代理机构", "开始日期", "结束日期",
			 "当前期次", "总期次", "本金余额",
			 "利息余额", "逾期本金余额", "逾期利息余额", "分期手续费余额",
			 "分期滞纳金余额", "资产状态", "逾期天数", "最近结息日期"],
			[userId, billID, 'loan', lenderOrgCode, managerOrgCode, disbDate, endDate, curTerm, termCnt, unpaidPrin,
			 unpaidIntr,
			 0, 0, 0, 0, "nor", 0, disbDate],
		])

	return

# 结息
def intrAdd():
	# 结息b
	intrDueDaily = round((float(totalAmt) * float(interRate)) / yearDays, 2)

	# 计算结息利息总额
	curIntrTotBal = totalIntrBal + intrDueDaily

	indexNum = str(index.getIndex())
	cratePath = basePath + 'testfile/intrAdd'

	os.path.join(basePath, '/testfile/', 'intrAdd', indexNum)
	os.mkdir(cratePath + indexNum)
	os.chdir(cratePath + indexNum)

	with open("文件总控_" + str(today) + ".csv", "w") as csvIntrList:
		writer = csv.writer(csvIntrList)
		writer.writerows([
			["文件名", "md5", "总数", "发生日期"],
			["账单_" + indexNum + ".csv", "md1", 10, today],
			["分期_" + indexNum + ".csv", "md1", 10, today],
			["账单交易_" + indexNum + ".csv", "md1", 10, today],
			["分期交易_" + indexNum + ".csv", "md1", 10, today],
		])

	counterNum = int(1)
	install_startDate = disbDate
	termList = []
	intrCal = totalIntrBal
	lastTerm = float(totalAmt)
	while counterNum <= int(termCnt):
		periodTime = date_time + 86400 * counterNum * int(repayPeriod)  # 一天86400秒
		period_timeDate = time.localtime(periodTime)
		install_endDate = time.strftime("%Y%m%d", period_timeDate)
		if intrCal >= intrAmt:
			intrBal = 0
		else:
			intrBal = intrCal
		intrCal = max(intrCal - intrAmt, 0)

		if counterNum < curTerm:
			paidTerm = 0
			intrBal = 0
		elif counterNum == curTerm:
			paidTerm = prinAmt
			intrBal = totalIntrBal - intrAmt * (curTerm - 1) + intrDueDaily
		elif counterNum > curTerm:
			paidTerm = prinAmt
			intrBal = 0

		if counterNum == int(termCnt):
			termAst = [billID, counterNum, install_startDate, install_endDate, intrAmt, 0, lastTerm, intrBal, 0, 0, 0,
			           0,
			           "nor", 0,
			           lenderOrgCode]
		else:
			termAst = [billID, counterNum, install_startDate, install_endDate, intrAmt, 0, paidTerm, intrBal, 0, 0, 0,
			           0,
			           "nor", 0,
			           lenderOrgCode]
		lastTerm = lastTerm - prinAmt
		termList.append(termAst)
		install_startDate = install_endDate
		counterNum = counterNum + 1

	with open("分期_" + indexNum + ".csv", "w") as csvAstCreat003:
		writer = csv.writer(csvAstCreat003)
		writer.writerows([
			                 ["账单id", "期次号", "开始日期", "结束日期", "到期利息", "到期费用",
			                  "本金余额", "利息余额", "逾期本金余额", "逾期利息余额",
			                  "分期手续费余额", "分期滞纳金余额", "资产状态", "逾期天数", "原始权益机构"]
		                 ] + termList)

	unpaidIntr = totalIntrBal - ((curTerm - 1) * intrAmt) + intrDueDaily
	unpaidPrin = int(totalAmt) - ((curTerm - 1) * prinAmt)

	with open("账单_" + indexNum + ".csv", "w") as csvAstCreat002:
		writer = csv.writer(csvAstCreat002)
		writer.writerows([
			["用户id", "账单id", "资产类型", "原始权益机构", "代理机构", "开始日期", "结束日期",
			 "当前期次", "总期次", "本金余额",
			 "利息余额", "逾期本金余额", "逾期利息余额", "分期手续费余额",
			 "分期滞纳金余额", "资产状态", "逾期天数", "最近结息日期"],
			[userId, billID, 'loan', lenderOrgCode, managerOrgCode, disbDate, endDate, curTerm, termCnt, unpaidPrin,
			 unpaidIntr,
			 0, 0, 0, 0, "nor", 0, disbDate],
		])

	with open("账单交易_" + indexNum + ".csv", "w") as csvIntrAdd003:
		writer = csv.writer(csvIntrAdd003)
		writer.writerows([
			["账单id", "当前期次", "外部业务编号", "交易时间", "会计日期", "交易码", "资产状态", "本金发生额", "利息发生额",
			 "逾期本金发生额", "逾期利息发生额", "分期手续费发生额", "分期滞纳金发生额", "原始权益机构"],
			[billID, curTerm, "outBizNo1", str(today) + str(time.strftime("%I%M%S")), today, "AST1004", "nor", 0,
			 intrDueDaily, 0, 0, 0, 0, lenderOrgCode], ])

	with open("分期交易_" + indexNum + ".csv", "w") as csvIntrAdd004:
		writer = csv.writer(csvIntrAdd004)
		writer.writerows([
			["账单id", "期次号", "外部业务编号", "外部流水号", "交易时间", "会计日期", "交易码", "资产状态", "本金发生额",
			 "利息发生额",
			 "逾期本金发生额", "逾期利息发生额", "分期手续费发生额", "分期滞纳金发生额", "原始权益机构"],
			[billID, curTerm, "outBizNo1", "outBizNo1001", str(today) + str(time.strftime("%I%M%S")), today, "AST1004",
			 "nor",
			 0, intrDueDaily, 0, 0, 0, 0, lenderOrgCode]
		])
	return

#转逾期
def turnOverDue():

	indexNum = str(index.getIndex())
	cratePath = basePath + 'testfile/turnOverDue'

	os.path.join(basePath, '/testfile/', 'turnOverDue', indexNum)
	os.mkdir(cratePath + indexNum)
	os.chdir(cratePath + indexNum)

	with open("文件总控_" + str(today) + ".csv", "w") as csvIntrList:
		writer = csv.writer(csvIntrList)
		writer.writerows([
			["文件名", "md5", "总数", "发生日期"],
			["账单_" + indexNum + ".csv", "md1", 10, today],
			["分期_" + indexNum + ".csv", "md1", 10, today],
			["账单交易_" + indexNum + ".csv", "md1", 10, today],
			["分期交易_" + indexNum + ".csv", "md1", 10, today],
		])

	counterNum = int(1)
	install_startDate = disbDate
	termList = []
	intrCal = totalIntrBal
	lastTerm = float(totalAmt)
	while counterNum <= int(termCnt):
		periodTime = date_time + 86400 * counterNum * int(repayPeriod)  # 一天86400秒
		period_timeDate = time.localtime(periodTime)
		install_endDate = time.strftime("%Y%m%d", period_timeDate)
		if intrCal >= intrAmt:
			intrBal = 0
		else:
			intrBal = intrCal
		intrCal = max(intrCal - intrAmt, 0)

		if counterNum < curTerm:
			paidTerm = 0
			intrBal = 0
		elif counterNum == curTerm:
			paidTerm = prinAmt
			intrBal = totalIntrBal - intrAmt * (curTerm - 1)
		elif counterNum > curTerm:
			paidTerm = prinAmt
			intrBal = 0

		if counterNum == int(termCnt):
			termAst = [billID, counterNum, install_startDate, install_endDate, intrAmt, 0, lastTerm, intrBal, 0, 0, 0,
			           0,
			           "ovd", 0,
			           lenderOrgCode]
		elif counterNum==curTerm:
			termAst = [billID, counterNum, install_startDate, install_endDate, intrAmt, 0, paidTerm, intrBal, 0, 0, 0,
			           0,
			           "ovd", 3,
			           lenderOrgCode]
		else:
			termAst = [billID, counterNum, install_startDate, install_endDate, intrAmt, 0, paidTerm, intrBal, 0, 0, 0,
			           0,
			           "ovd", 0,
			           lenderOrgCode]
		lastTerm = lastTerm - prinAmt
		termList.append(termAst)
		install_startDate = install_endDate
		counterNum = counterNum + 1

	with open("分期_" + indexNum + ".csv", "w") as csvAstCreat003:
		writer = csv.writer(csvAstCreat003)
		writer.writerows([
			                 ["账单id", "期次号", "开始日期", "结束日期", "到期利息", "到期费用",
			                  "本金余额", "利息余额", "逾期本金余额", "逾期利息余额",
			                  "分期手续费余额", "分期滞纳金余额", "资产状态", "逾期天数", "原始权益机构"]
		                 ] + termList)

	unpaidIntr = totalIntrBal - ((curTerm - 1) * intrAmt)
	unpaidPrin = int(totalAmt) - ((curTerm - 1) * prinAmt)

	with open("账单_" + indexNum + ".csv", "w") as csvAstCreat002:
		writer = csv.writer(csvAstCreat002)
		writer.writerows([
			["用户id", "账单id", "资产类型", "原始权益机构", "代理机构", "开始日期", "结束日期",
			 "当前期次", "总期次", "本金余额",
			 "利息余额", "逾期本金余额", "逾期利息余额", "分期手续费余额",
			 "分期滞纳金余额", "资产状态", "逾期天数", "最近结息日期"],
			[userId, billID, 'loan', lenderOrgCode, managerOrgCode, disbDate, endDate, curTerm, termCnt, unpaidPrin,
			 unpaidIntr,
			 0, 0, 0, 0, "ovd", 3, disbDate],
		])

	curTime = date_time + (86400 * curTerm * int(repayPeriod)+86400*3)  # 一天86400秒
	cur_timeDate = time.localtime(curTime)
	cur_endDate = time.strftime("%Y%m%d", cur_timeDate)
	with open("分期交易_" + indexNum + ".csv", "w") as csvIntrAdd004:
		writer = csv.writer(csvIntrAdd004)
		writer.writerows([
			["账单id", "期次号", "外部业务编号", "外部流水号", "交易时间", "会计日期", "交易码", "资产状态", "本金发生额",
			 "利息发生额",
			 "逾期本金发生额", "逾期利息发生额", "分期手续费发生额", "分期滞纳金发生额", "原始权益机构"],
			[billID, curTerm, "outBizNo1", "outBizNo1001", str(today) + str(time.strftime("%I%M%S")), cur_endDate, "AST1005",
			 "ovd",
			 unpaidPrin, curIntrBal, 0, 0, 0, 0, lenderOrgCode]
		])

	with open("账单交易_" + indexNum + ".csv", "w") as csvIntrAdd003:
		writer = csv.writer(csvIntrAdd003)
		writer.writerows([
			["账单id", "当前期次", "外部业务编号", "交易时间", "会计日期", "交易码", "资产状态", "本金发生额", "利息发生额",
			 "逾期本金发生额", "逾期利息发生额", "分期手续费发生额", "分期滞纳金发生额", "原始权益机构"],
			[billID, curTerm, "outBizNo1", str(today) + str(time.strftime("%I%M%S")), cur_endDate, "AST1005", "ovd",
			 unpaidPrin,
			 unpaidIntr, 0, 0, 0, 0, lenderOrgCode], ])


	return
#还款
def payTerm():

	indexNum = str(index.getIndex())
	cratePath = basePath + 'testfile/payTerm'

	os.path.join(basePath, '/testfile/', 'payTerm', indexNum)
	os.mkdir(cratePath + indexNum)
	os.chdir(cratePath + indexNum)

	with open("文件总控_" + str(today) + ".csv", "w") as csvIntrList:
		writer = csv.writer(csvIntrList)
		writer.writerows([
			["文件名", "md5", "总数", "发生日期"],
			["账单_" + indexNum + ".csv", "md1", 10, today],
			["分期_" + indexNum + ".csv", "md1", 10, today],
			["账单交易_" + indexNum + ".csv", "md1", 10, today],
			["分期交易_" + indexNum + ".csv", "md1", 10, today],
		])

	counterNum = int(1)
	install_startDate = disbDate
	termList = []
	intrCal = totalIntrBal
	lastTerm = float(totalAmt)
	while counterNum <= int(termCnt):
		periodTime = date_time + 86400 * counterNum * int(repayPeriod)  # 一天86400秒
		period_timeDate = time.localtime(periodTime)
		install_endDate = time.strftime("%Y%m%d", period_timeDate)
		if intrCal >= intrAmt:
			intrBal = 0
		else:
			intrBal = intrCal
		intrCal = max(intrCal - intrAmt, 0)

		if counterNum <= curTerm:
			paidTerm = 0
			intrBal = 0
		elif counterNum > curTerm:
			paidTerm = prinAmt
			intrBal = 0

		if counterNum == int(termCnt):
			termAst = [billID, counterNum, install_startDate, install_endDate, intrAmt, 0, lastTerm, intrBal, 0, 0, 0,
			           0,
			           "nor", 0,
			           lenderOrgCode]
		else:
			termAst = [billID, counterNum, install_startDate, install_endDate, intrAmt, 0, paidTerm, intrBal, 0, 0, 0,
			           0,
			           "nor", 0,
			           lenderOrgCode]
		lastTerm = lastTerm - prinAmt
		termList.append(termAst)
		install_startDate = install_endDate
		counterNum = counterNum + 1

	with open("分期_" + indexNum + ".csv", "w") as csvAstCreat003:
		writer = csv.writer(csvAstCreat003)
		writer.writerows([
			                 ["账单id", "期次号", "开始日期", "结束日期", "到期利息", "到期费用",
			                  "本金余额", "利息余额", "逾期本金余额", "逾期利息余额",
			                  "分期手续费余额", "分期滞纳金余额", "资产状态", "逾期天数", "原始权益机构"]
		                 ] + termList)
	#已结利息全部还清
	unpaidIntr = 0
	unpaidPrin = int(totalAmt) - (curTerm * prinAmt)

	with open("账单_" + indexNum + ".csv", "w") as csvAstCreat002:
		writer = csv.writer(csvAstCreat002)
		writer.writerows([
			["用户id", "账单id", "资产类型", "原始权益机构", "代理机构", "开始日期", "结束日期",
			 "当前期次", "总期次", "本金余额",
			 "利息余额", "逾期本金余额", "逾期利息余额", "分期手续费余额",
			 "分期滞纳金余额", "资产状态", "逾期天数", "最近结息日期"],
			[userId, billID, 'loan', lenderOrgCode, managerOrgCode, disbDate, endDate, curTerm, termCnt, unpaidPrin,
			 unpaidIntr,
			 0, 0, 0, 0, "nor", 0, disbDate],
		])

	with open("账单交易_" + indexNum + ".csv", "w") as csvIntrAdd003:
		writer = csv.writer(csvIntrAdd003)
		writer.writerows([
			["账单id", "当前期次", "外部业务编号", "交易时间", "会计日期", "交易码", "资产状态", "本金发生额", "利息发生额",
			 "逾期本金发生额", "逾期利息发生额", "分期手续费发生额", "分期滞纳金发生额", "原始权益机构"],
			[billID, curTerm, "outBizNo1", str(today) + str(time.strftime("%I%M%S")), today, "AST1003", "nor", prinAmt,
			 curIntrBal, 0, 0, 0, 0, lenderOrgCode], ])

	with open("分期交易_" + indexNum + ".csv", "w") as csvIntrAdd004:
		writer = csv.writer(csvIntrAdd004)
		writer.writerows([
			["账单id", "期次号", "外部业务编号", "外部流水号", "交易时间", "会计日期", "交易码", "资产状态", "本金发生额",
			 "利息发生额",
			 "逾期本金发生额", "逾期利息发生额", "分期手续费发生额", "分期滞纳金发生额", "原始权益机构"],
			[billID, curTerm, "outBizNo1", "outBizNo1001", str(today) + str(time.strftime("%I%M%S")), today, "AST1003",
			 "nor",
			 prinAmt, curIntrBal, 0, 0, 0, 0, lenderOrgCode]
		])
	return

def clrTerm():

	indexNum = str(index.getIndex())
	cratePath = basePath + 'testfile/clrTerm'

	os.path.join(basePath, '/testfile/', 'clrTerm', indexNum)
	os.mkdir(cratePath + indexNum)
	os.chdir(cratePath + indexNum)

	with open("文件总控_" + str(today) + ".csv", "w") as csvIntrList:
		writer = csv.writer(csvIntrList)
		writer.writerows([
			["文件名", "md5", "总数", "发生日期"],
			["账单_" + indexNum + ".csv", "md1", 10, today],
			["分期_" + indexNum + ".csv", "md1", 10, today],
			["账单交易_" + indexNum + ".csv", "md1", 10, today],
			["分期交易_" + indexNum + ".csv", "md1", 10, today],
		])

	counterNum = int(1)
	install_startDate = disbDate
	termList = []
	while counterNum <= int(termCnt):
		periodTime = date_time + 86400 * counterNum * int(repayPeriod)  # 一天86400秒
		period_timeDate = time.localtime(periodTime)
		install_endDate = time.strftime("%Y%m%d", period_timeDate)

		termAst = [billID, counterNum, install_startDate, install_endDate, intrAmt, 0, 0, 0, 0, 0, 0,
			           0,
			           "clr", 0,
			           lenderOrgCode]

		termList.append(termAst)
		install_startDate = install_endDate
		counterNum = counterNum + 1

	with open("分期_" + indexNum + ".csv", "w") as csvAstCreat003:
		writer = csv.writer(csvAstCreat003)
		writer.writerows([
			                 ["账单id", "期次号", "开始日期", "结束日期", "到期利息", "到期费用",
			                  "本金余额", "利息余额", "逾期本金余额", "逾期利息余额",
			                  "分期手续费余额", "分期滞纳金余额", "资产状态", "逾期天数", "原始权益机构"]
		                 ] + termList)

	with open("账单_" + indexNum + ".csv", "w") as csvAstCreat002:
		writer = csv.writer(csvAstCreat002)
		writer.writerows([
			["用户id", "账单id", "资产类型", "原始权益机构", "代理机构", "开始日期", "结束日期",
			 "当前期次", "总期次", "本金余额",
			 "利息余额", "逾期本金余额", "逾期利息余额", "分期手续费余额",
			 "分期滞纳金余额", "资产状态", "逾期天数", "最近结息日期"],
			[userId, billID, 'loan', lenderOrgCode, managerOrgCode, disbDate, endDate, curTerm, termCnt, 0,
			 0,
			 0, 0, 0, 0, "clr", 0, disbDate],
		])

	#全部还清
	unpaidPrin = int(totalAmt) - ((curTerm-1) * prinAmt)
	with open("账单交易_" + indexNum + ".csv", "w") as csvClr003:
		writer = csv.writer(csvClr003)
		writer.writerows([
			["账单id", "当前期次", "外部业务编号", "交易时间", "会计日期", "交易码", "资产状态", "本金发生额", "利息发生额",
			 "逾期本金发生额", "逾期利息发生额", "分期手续费发生额", "分期滞纳金发生额", "原始权益机构"],
			[billID, curTerm, "outBizNo1", str(today) + str(time.strftime("%I%M%S")), today, "AST1003", "clr", unpaidPrin,
			 curIntrBal, 0, 0, 0, 0, lenderOrgCode], ])

	payTermNum = int(1)
	payList = []
	lastTerm = float(totalAmt)
	while payTermNum <= int(termCnt):
		if payTermNum < curTerm:
			paidTerm = 0
			intrBal = 0
		elif payTermNum == curTerm:
			paidTerm = prinAmt
			intrBal = curIntrBal
			print(paidTerm,curIntrBal)
		elif payTermNum > curTerm:
			paidTerm = prinAmt
			intrBal = 0

		if payTermNum == int(termCnt):
			termAst = [billID, payTermNum, "outBizNo1", str(today) + str(time.strftime("%I%M%S")),today, "AST1003","clr", lastTerm, intrBal, 0, 0, 0,
			           0,lenderOrgCode]
		else:
			termAst = [billID, payTermNum, "outBizNo1", str(today) + str(time.strftime("%I%M%S")),today, "AST1003","clr", paidTerm, intrBal, 0, 0, 0,
			           0,lenderOrgCode]
		lastTerm = lastTerm - prinAmt
		payList.append(termAst)
		payTermNum = payTermNum + 1

	with open("分期交易_" + indexNum + ".csv", "w") as csvClr004:
		writer = csv.writer(csvClr004)
		writer.writerows([
			["账单id", "期次号", "外部业务编号", "外部流水号", "交易时间", "会计日期", "交易码", "资产状态", "本金发生额",
			 "利息发生额",
			 "逾期本金发生额", "逾期利息发生额", "分期手续费发生额", "分期滞纳金发生额", "原始权益机构"],
			]+payList)
	return


#两个文件同时生成时，billID是一样的
if int(testPurpose) == 1:
	astCreat()
elif int(testPurpose) == 2:
	astCreat()
	intrAdd()
elif int(testPurpose) == 3:
	astCreat()
	turnOverDue()
elif int(testPurpose) == 4:
	astCreat()
	payTerm()
elif int(testPurpose) == 5:
	astCreat()
	clrTerm()
